package com.feeyo.groundservice.recordnfc.dlg

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.lifecycle.setViewTreeLifecycleOwner
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.savedstate.setViewTreeSavedStateRegistryOwner
import com.blankj.utilcode.util.ScreenUtils
import com.feeyo.groundservice.base_util.dealString
import com.feeyo.groundservice.composewidget.util.EmptyListView
import com.feeyo.groundservice.composewidget.util.dimenToDp
import com.feeyo.groundservice.composewidget.util.dimenToSp
import com.feeyo.groundservice.composewidget.util.toComposeColor
import com.feeyo.groundservice.composewidget.view.util.VerticalSpace
import com.feeyo.groundservice.recordnfc.R
import com.feeyo.groundservice.recordnfc.databinding.ViewCommonPopBinding
import com.feeyo.groundservice.recordnfc.model.NfcLogItemBO
import com.feeyo.groundservice.recordnfc.viewmodel.RecorderCardViewModel
import com.lxj.xpopup.core.BottomPopupView

/**
 *作者：70893 on 2025-10-13
 *描述：
 */
internal class NfcLogPopView(context: Context) : BottomPopupView(context) {

    init {
        id = android.R.id.content
        setViewTreeLifecycleOwner(context as AppCompatActivity)
        setViewTreeSavedStateRegistryOwner(context as AppCompatActivity)
    }

    private lateinit var binding: ViewCommonPopBinding
    override fun getImplLayoutId(): Int {
        return R.layout.view_common_pop
    }

    override fun onCreate() {
        super.onCreate()
        binding = ViewCommonPopBinding.bind(this.popupImplView)
        binding.popCp.apply {
            setContent {
                val mContext = LocalContext.current
                val viewModel: RecorderCardViewModel = viewModel(mContext as AppCompatActivity)

                val data = viewModel.nfcLogList.value ?: emptyList()

                Column(
                    modifier = Modifier.fillMaxWidth().background(
                        color=R.color.white.toComposeColor(),
                        shape = RoundedCornerShape(R.dimen.d8.dimenToDp())
                    )
                ) {
                    Row(
                        modifier = Modifier.fillMaxWidth().padding(
                            horizontal = R.dimen.d15.dimenToDp(),
                            vertical = R.dimen.d10.dimenToDp()
                        ),
                        horizontalArrangement = Arrangement.SpaceBetween,
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Text(
                            text = "今日录入记录",
                            fontSize = R.dimen.d15.dimenToSp(),
                            fontWeight = FontWeight.W500,
                            color = R.color.black_33.toComposeColor()
                        )

                        Text(
                            text = "数量: ${data.size}",
                            fontSize = R.dimen.d16.dimenToSp(),
                            fontWeight = FontWeight.Bold
                        )
                    }


                    LazyColumn(
                        modifier = Modifier.fillMaxSize(),
                        verticalArrangement = Arrangement.spacedBy(R.dimen.d5.dimenToDp())
                    ) {
                        item {
                            VerticalSpace(R.dimen.d1.dimenToDp())
                        }
                        if (data.isEmpty()) {
                            item {
                                Box(
                                    modifier = Modifier.fillMaxWidth()
                                        .height(R.dimen.d200.dimenToDp())
                                        .background(R.color.white.toComposeColor()),
                                    contentAlignment = Alignment.Center
                                ) {
                                    EmptyListView()
                                }
                            }
                        } else {
                            items(data, key = { it.id }) {
                                NfcLogItemView(it)
                            }
                        }
                        item {
                            VerticalSpace(R.dimen.d1.dimenToDp())
                        }
                    }


                }

                LaunchedEffect(Unit) {
                    viewModel.getNfcLog()
                }
            }
        }
    }

    override fun getMaxHeight(): Int {
        return (ScreenUtils.getScreenHeight() * 0.75).toInt()
    }

    @Composable
    private fun NfcLogItemView(item: NfcLogItemBO) {
        Card(
            modifier = Modifier.fillMaxWidth().padding(horizontal = R.dimen.d15.dimenToDp()),
            elevation = CardDefaults.elevatedCardElevation(defaultElevation = R.dimen.d5.dimenToDp()),
            shape = RoundedCornerShape(R.dimen.d4.dimenToDp()),
            colors = CardDefaults.elevatedCardColors(containerColor = R.color.white.toComposeColor())
        ) {

            Column(
                modifier = Modifier.fillMaxWidth().padding(R.dimen.d10.dimenToDp())
            ) {
                Text(
                    text = item.msg.dealString(),
                    fontSize = R.dimen.d14.dimenToSp(),
                    fontWeight = FontWeight.W400,
                    color = R.color.black_33.toComposeColor(),
                )


                VerticalSpace(R.dimen.d5.dimenToDp())
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = item.nfc.dealString(),
                        fontSize = R.dimen.d14.dimenToSp(),
                        fontWeight = FontWeight.W400,
                        color = R.color.black_33.toComposeColor(),
                    )

                    Text(
                        text = item.recorder,
                        fontSize = R.dimen.d14.dimenToSp(),
                        fontWeight = FontWeight.W400,
                        color = R.color.black_66.toComposeColor(),
                    )
                }
            }
        }
    }
}